
Partial Class Modules_Questionnaires_Controls_ansNumber
    Inherits UserControl
    Implements DotNetPm.Business.Questionnaire.Taker.IAnswerUC

    Private _config, _answerType, _questionId, _sortId As String

    Public Property Answer() As String Implements DotNetPm.Business.Questionnaire.Taker.IAnswerUC.Answer
        Get
            Return txtAnswer.Text
        End Get
        Set(ByVal value As String)
            If String.IsNullOrEmpty(value) Then
                Exit Property
            End If
            txtAnswer.Text = value
        End Set
    End Property

    Public Property Config() As String Implements DotNetPm.Business.Questionnaire.Taker.IAnswerUC.Config
        Get
            Return _config
        End Get
        Set(ByVal value As String)
            If Not String.IsNullOrEmpty(value) Then
                _config = value
            End If
        End Set
    End Property

    Private Sub LoadAnswerControl()
        Dim val As String() = Split(Me.Config, "||")
        _sortId = val(0)
        _questionId = val(1)
        If val(2) = "2" Then
            _answerType = "Float"
        Else
            _answerType = "Integer"
        End If
        If val(3) = "0" Then
            Dim reqVal As New RequiredFieldValidator
            With reqVal
                .ID = "reqVal"
                .ControlToValidate = "txtAnswer"
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "The answer of " & _answerType & " type is required."
                '.ErrorMessage = "*<br/>The answer of " & _answerType & " is required."
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : The answer of " & _answerType & " is required."
            End With
            phValidator.Controls.Add(reqVal)
        End If
        Dim _Min As String = val(4)
        Dim _Max As String = val(5)
        If Not String.IsNullOrEmpty(_Min) AndAlso Not String.IsNullOrEmpty(_Max) Then
            Dim minVal As New RangeValidator
            With minVal
                .ID = "minmaxVal"
                .ControlToValidate = "txtAnswer"
                If _answerType = "Float" Then
                    .Type = ValidationDataType.Double
                    .MinimumValue = CDbl(_Min)
                    .MaximumValue = CDbl(_Max)
                Else
                    .Type = ValidationDataType.Integer
                    .MinimumValue = CInt(_Min)
                    .MaximumValue = CInt(_Max)
                End If
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "Invalid Value for the answer of " & _answerType & "! The valid value is between " & _Min & " to " & _Max & "."
                '.ErrorMessage = "*<br/>Invalid Value for the answer of " & _answerType & ", the valid value is between " & _Min & " to " & _Max & "."
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Value for the answer of " & _answerType & ", the valid value is between " & _Min & " to " & _Max & "."

            End With
            phValidator.Controls.Add(minVal)
        ElseIf Not String.IsNullOrEmpty(_Min) Then
            Dim minVal As New CompareValidator
            With minVal
                .ID = "minVal"
                .ControlToValidate = "txtAnswer"
                .Operator = ValidationCompareOperator.GreaterThanEqual
                If _answerType = "Float" Then
                    .Type = ValidationDataType.Double
                    .ValueToCompare = CDbl(_Min)
                Else
                    .Type = ValidationDataType.Integer
                    .ValueToCompare = CInt(_Min)
                End If
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "Invalid Value for the answer of " & _answerType & "! The minimum value is " & _Min & "."
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Value for the answer of " & _answerType & ", the minimum value is " & _Min & "."
            End With
            phValidator.Controls.Add(minVal)
        ElseIf Not String.IsNullOrEmpty(_Max) Then
            Dim maxVal As New CompareValidator
            With maxVal
                .ID = "maxVal"
                .ControlToValidate = "txtAnswer"
                .Operator = ValidationCompareOperator.LessThanEqual
                If _answerType = "Float" Then
                    .Type = ValidationDataType.Double
                    .ValueToCompare = CDbl(_Max)
                Else
                    .Type = ValidationDataType.Integer
                    .ValueToCompare = CInt(_Max)
                End If
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "Invalid Maximum Value for the answer of " & _answerType & "! The maximum value is " & _Max & "."
                '.ErrorMessage = "*<br/>Invalid Maximum Value for the answer of " & _answerType & ", the maximum value is " & _Max & "."
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Maximum Value for the answer of " & _answerType & ", the maximum value is " & _Max & "."
            End With
            phValidator.Controls.Add(maxVal)
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadAnswerControl()
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        'LoadAnswerControl()
    End Sub
End Class
